'use strict';

function constructionTeamCtrl($rootScope, $scope, $http, ngDialog, paFetchService) {
  'ngInject';

  this.units = [];
  paFetchService.getOrgList().then((resp) => {
    this.units = resp.data;
  });
  this.constructionTeamlist = [];
  this.construction = {};

  // 分页
  this.paginationData = {
    pageNum: 1,
    pageSize: 10,
    total: '',
  }

  // 页码变化时调用方法
  $scope.pageChange = () => {
    $http.post(`${SERVER_CONTEXT}/api/construction/select/s`,
      angular.extend(this.construction, this.paginationData)
    ).
    then((resp) => {
      this.constructionTeamlist = resp.data.data;
      this.paginationData.total = resp.data.total;
    });
  };

  // 查询施工队
  this.queryConstruction = () => {
    $http.post(`${SERVER_CONTEXT}/api/construction/select/s`,
      angular.extend(this.construction, this.paginationData)
    ).
    then((resp) => {
      this.constructionTeamlist = resp.data.data;
      this.paginationData.total = resp.data.total;
    });
  };

  // this.queryConstruction();
  
  // 根据条件查询施工队
  this.teamSearch = () => {
    this.queryConstruction();
  };
  // 重置查询信息
  this.reset = () => {
    this.construction = {};
  };
  // 新增施工队信息
  this.newTeam = () => {
    let _this = this;
    ngDialog.open({
      template: 'newTeam',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      scope: $scope,
      controllerAs: 'cfgAdd',
      controller: function cfgAddCtrl($http, $scope) {
        'ngInject';
        this.addItem = {
          constructionTeamName: '',
          pid: ''
        };
        this.submit = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }
          let reqData = this.addItem;
          $http({
            method: 'POST',
            url: `${SERVER_CONTEXT}/api/construction/insert`,
            data: reqData
          }).then((resp) => {
            if (resp.data.success) {
              _this.queryConstruction();
              $scope.closeThisDialog();
              layer.msg(resp.data.message);
            } else {
              layer.alert(resp.data.message);
              $scope.closeThisDialog();
            }
          });
        }
      },
    });
  };
  // 编辑施工队信息
  this.editConstructionItem = [];
  this.openConstructionTeam = (id) => {
      $scope.id = id;
      ngDialog.open({
        template: 'openTeam',
        className: 'ngdialog-theme-default pa-special-check-item-list-theme',
        scope: $scope,
        controller: function($scope) {
          'ngInject';
          let str = `${SERVER_CONTEXT}/api/construction/select/s/${$scope.id}`;
          $http.get(str).
          then((resp) => {
            $scope.editConstructionItem = resp.data;
            $scope.editConstructionItem.id = id;
          });
          $scope.submit = (param) => {
            // 修改表单是否提交过状态
            param.$submitted = true;

            // 表单校验
            if (param.$invalid) {
              return false;
            }

            $http({
              method: 'POST',
              url: `${SERVER_CONTEXT}/api/construction/update`,
              data: $scope.editConstructionItem,
            }).then((resp) => {
              if (resp.statusText == 'OK') {
                $scope.constructionTeam.queryConstruction();
                resp.data.message == '更新成功' ? layer.msg("信息更新成功") : layer.alert("信息更新失败");
                $scope.closeThisDialog();
              } else {
                $scope.closeThisDialog();
              }
            });
          }
        }
      });
    }
    // 删除施工队信息
  this.deleteConstructionItem = (param) => {
    let _this = this;
    $scope.modalTitle = '删除施工队信息';
    $scope.modalContent = '确认要删除该施工队信息吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
    then((value) => {
      if (param.num > 0) {
        layer.msg('请先删除施工队相关人员');
      } else {
        $http({
          method: 'DELETE',
          url: `${SERVER_CONTEXT}/api/construction/delete/${param.id}`
        }).
        then((data) => {
          _this.queryConstruction();
        });
      }
    }, (reason) => {});
  };
}

export default constructionTeamCtrl;